package com.fancy.controller.web;

import com.fancy.emums.StatusEnum;
import com.fancy.util.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 登录校验异常
 * Created by FancyFong on 9/9/2017.
 */

@Controller
@RequestMapping("/loginController")
public class LoginController {

    @Autowired
    private StringRedisTemplate redicTemplate;

    @RequestMapping(params = "login")
    public ModelAndView productInfoPage(@RequestParam("userName") String userName,
                                        @RequestParam("password") String password,
                                        HttpServletResponse response,
                                        Map<String,Object> map){
        //登录校验
        if(!userName.equals("fancy")&&!password.equals("123455")){
            map.put("msg", StatusEnum.LOGIN_ERROR.getMessage());
            return new ModelAndView("common/error",map);
        }


        //1.登录成功，设置token到redis
        String token = UUID.randomUUID().toString().replace("-","");
        Integer expire = 7200;

        redicTemplate.opsForValue().set("token_"+token,userName,expire, TimeUnit.SECONDS);

        //2.设置token到cokie
        CookieUtil.set(response,"token",token,expire);


        return new ModelAndView("web/productInfo/productInfoList");
    }


}
